using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LogAnalyzerIIS.Objects;

namespace LogAnalyzerIIS.WizardForms
{
	public partial class SessionForm : Form
	{
		private Session session;

		private List<string> existingSessionNames;

		/// <summary>
		/// Initializes a new session adding form
		/// </summary>
		/// <param name="currentSession">The session that is being added</param>
		/// <param name="sessionNames">List of session names that exist already</param>
		public SessionForm(Session currentSession, List<string> sessionNames)
		{
			InitializeComponent();

			if (currentSession == null)
				session = new Session();
			else
				session = currentSession;

			tbSessionName.Text = session.Name;
			tbDownloadFolder.Text = session.DownloadFolder;
			tbOutputFolder.Text = session.OutputFolder;
			existingSessionNames = sessionNames;
		}

		/// <summary>
		/// Loads controls values from the session object
		/// </summary>
		private void LoadControlsFromSession()
		{
			tbSessionName.Text = session.Name;
			tbDownloadFolder.Text = session.DownloadFolder;
			tbOutputFolder.Text = session.OutputFolder;
		}

		/// <summary>
		/// Saves control valeus to the session fields
		/// </summary>
		private void SaveControlsToSession()
		{
			session.Name = tbSessionName.Text;
			session.DownloadFolder = tbDownloadFolder.Text;
			session.OutputFolder = tbOutputFolder.Text;
		}

		private void DownloadFolderBrowser_Click(object sender, EventArgs e)
		{
			if (downloadFolderBrowser.ShowDialog() == DialogResult.OK)
				tbDownloadFolder.Text = downloadFolderBrowser.SelectedPath;
		}

		private void OutputFolderBrowser_Click(object sender, EventArgs e)
		{
			if (OutputFolderBrowser.ShowDialog() == DialogResult.OK)
				tbOutputFolder.Text = OutputFolderBrowser.SelectedPath;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			// check if the name that user entered already exists
			if (existingSessionNames.Contains(tbSessionName.Text))
			{
				MessageBox.Show("The session with this name already exists");
				return;
			}
			// save the session
			if (tbDownloadFolder.Text != "" && tbOutputFolder.Text != "" && tbSessionName.Text != "")
			{
				SaveControlsToSession();

				session.Save();

				this.DialogResult = DialogResult.OK;

				this.Close();
			}
			else
				MessageBox.Show("Please enter a name, a download folder and an output folder");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}

}